<?php
/**
 * Created by PhpStorm.
 * User: eddy
 * Date: 2018/3/13
 * Time: 23:40
 */

namespace iBrand\Shop\Backend\Http\Controllers;

use iBrand\Component\Discount\Models\Coupon;
use iBrand\Component\Point\Model\Point;
use iBrand\Shop\Core\Repositories\OrderRepository;
use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;

class OrderController extends Controller
{
	protected $orderRepository;

	public function __construct(OrderRepository $orderRepository)
	{
		$this->orderRepository = $orderRepository;
	}

	public function index()
	{
		$shop_id                       = session('shop_id');
		$orderConditions['channel']    = 'shop';
		$orderConditions['channel_id'] = $shop_id;

		if (request('user_id')) {
			$orderConditions['user_id'] = request('user_id');
		}

		if (!empty($pay_status = request('pay_status'))) {
			$orderConditions['pay_status'] = $pay_status == 'paid' ? 1 : 0;
		}

		$mobile = '';
		if ($filed = request('field') AND $value = request('value')) {
			if ($filed == 'order_no') {
				$orderConditions['order_no'] = $value;
			} else {
				$mobile = $value;
			}
		}

		if (!empty(request('etime'))) {
			$orderConditions['created_at'] = ['created_at', '<=', request('etime')];
		}
		if (!empty(request('stime'))) {
			$orderConditions['created_at'] = ['created_at', '>=', request('stime')];
		}

		$between=[];
		if (!empty(request('etime')) && !empty(request('stime'))) {
			$between = [request('stime'), request('etime')];
		}

		if (request('status')) {
			$orderConditions['status'] = request('status');
		} else {
			$orderConditions ['status']  = ['status', '<>', 0];
			$orderConditions ['status2'] = ['status', '<>', 9];
		}

		$orders = $this->orderRepository->getOrdersByConditions($orderConditions, [], 15, ['items'], 6, 0, $mobile, 1,$between);

		return LaravelAdmin::content(function (Content $content) use ($orders, $shop_id) {

			$content->header('订单列表');

			if (session()->has('shop_name')) {

				$content->description(session('shop_name'));
			}

			$content->breadcrumb(
				['text' => '门店管理', 'url' => 'shop/list/store', 'no-pjax' => 1],
				['text' => '订单列表', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '订单列表']
			);

			$content->body(view('shop-backend::orders.index', compact('orders', 'shop_id')));
		});
	}

	public function show($id)
	{
		if (!session()->has('shop_name') || !session()->has('shop_id')) {
			return redirect()->route('admin.shop.list.store');
		}

		$order       = $this->orderRepository->find($id);
		$adjustments = $order->adjustments()->get();
		foreach ($adjustments as $item) {
			$discount          = Coupon::find($item->origin_id);
			$item->coupon_code = isset($discount->code) ? $discount->code : '';
		}

		$item_id    = [];
		$orderPoint = [];
		foreach ($order->items as $item) {
			$item_id[] = $item->id;
		}
		if (isset($item_id)) {
			$orderPoint = Point::where(['action' => 'order_item'])->whereIn('item_id', $item_id)->get();
		}

		$orderConsumePoint = Point::where('action', 'order_discount')->where('item_id', $id)->get();

		return LaravelAdmin::content(function (Content $content) use ($order, $adjustments, $orderPoint, $orderConsumePoint) {

			$content->header('订单详情');

			$content->description(session('shop_name'));

			$content->breadcrumb(
				['text' => '订单列表', 'url' => 'shop/info/order?shop_id=', 'no-pjax' => 1],
				['text' => '订单详情', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '订单列表']
			);

			$content->body(view('shop-backend::orders.show', compact('order', 'adjustments', 'orderPoint', 'orderConsumePoint')));
		});
	}
}